using System;
using System.Diagnostics;
using System.Windows.Forms;
using FilteredStatistics.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;

namespace FilteredStatistics.View
{
    partial class SplitsCriteriaSelectionDialog : Form
    {
        public SplitsCriteriaSelectionDialog(IActivity activity, SplitsPlaceholderFilterCriteria placeholderCriteria)
        {
            InitializeComponent();

            m_Activity = activity;
            m_PlaceholderCriteria = placeholderCriteria;

            this.Text = PluginView.GetLocalizedString("SelectSplitTypeText");

            RecordedLapsRadioButton.Text = PluginView.GetLocalizedString("RecordedLapsText");
            RecordedLapsRadioButton.Checked = true;

            CustomDistanceRadioButton.Text = PluginView.GetLocalizedString("CustomDistanceLapsText");

            CustomTimeRadioButton.Text = PluginView.GetLocalizedString("CustomTimeLapsText");

            DistanceUnitComboBox.Items.Add(Length.Units.Meter);
            DistanceUnitComboBox.Items.Add(Length.Units.Kilometer);
            DistanceUnitComboBox.Items.Add(Length.Units.Yard);
            DistanceUnitComboBox.Items.Add(Length.Units.Mile);
            DistanceUnitComboBox.SelectedItem = Utils.MajorLengthUnit(m_Activity.Category.DistanceUnits);
            DistanceUnitComboBox.Format += new ListControlConvertEventHandler(DistanceUnitComboBox_Format);

            DistanceTextBox.Text = "1";

            OKButton.Text = CommonResources.Text.ActionOk;
            Cancel_Button.Text = CommonResources.Text.ActionCancel;
        }

        void DistanceUnitComboBox_Format(object sender, ListControlConvertEventArgs e)
        {
            Length.Units item = (Length.Units)e.ListItem;

            e.Value = Length.LabelAbbr(item);
        }

#region UI callbacks

        private void OKButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void SplitTypeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            DistanceTextBox.Enabled = CustomDistanceRadioButton.Checked;
            DistanceUnitComboBox.Enabled = CustomDistanceRadioButton.Checked;

            LapTimeDurationUpDown.Enabled = CustomTimeRadioButton.Checked;
        }

        private void DistanceTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            float distanceEntered;

            if (!float.TryParse(DistanceTextBox.Text, out distanceEntered) || distanceEntered <= 0)
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidDistanceEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

#endregion

        public IFilterCriteria SelectedCriteria
        {
            get
            {
                if (RecordedLapsRadioButton.Checked)
                {
                    return new RecordedLapsFilterCriteria(m_Activity);
                }
                else if (CustomDistanceRadioButton.Checked)
                {
                    if (m_PreviousCriteria is CustomDistanceLapsFilterCriteria)
                    {
                        CustomDistanceLapsFilterCriteria previousCriteria = m_PreviousCriteria as CustomDistanceLapsFilterCriteria;

                        if (float.Parse(DistanceTextBox.Text) == previousCriteria.LapDistance &&
                            (Length.Units)DistanceUnitComboBox.SelectedItem == previousCriteria.DistanceUnits)
                        {
                            // Return original, nothing has changed
                            return previousCriteria;
                        }
                    }

                    return new CustomDistanceLapsFilterCriteria(m_Activity, float.Parse(DistanceTextBox.Text), (Length.Units)DistanceUnitComboBox.SelectedItem);
                }
                else
                {
                    if (m_PreviousCriteria is CustomTimeLapsFilterCriteria)
                    {
                        CustomTimeLapsFilterCriteria previousCriteria = m_PreviousCriteria as CustomTimeLapsFilterCriteria;

                        if (LapTimeDurationUpDown.Duration == (int)previousCriteria.LapTime.TotalSeconds)
                        {
                            // Return original, nothing has changed
                            return previousCriteria;
                        }
                    }

                    return new CustomTimeLapsFilterCriteria(m_Activity, new TimeSpan(0, 0, LapTimeDurationUpDown.Duration));
                }
            }
        }

        public IFilterCriteria PreviousCriteria
        {
            set
            {
                if (value is CustomDistanceLapsFilterCriteria)
                {
                    CustomDistanceLapsFilterCriteria criteria = value as CustomDistanceLapsFilterCriteria;

                    CustomDistanceRadioButton.Checked = true;
                    DistanceTextBox.Text = criteria.LapDistance.ToString("0.0");
                    DistanceUnitComboBox.SelectedItem = criteria.DistanceUnits;
                }
                else if (value is CustomTimeLapsFilterCriteria)
                {
                    CustomTimeLapsFilterCriteria criteria = value as CustomTimeLapsFilterCriteria;

                    CustomTimeRadioButton.Checked = true;
                    LapTimeDurationUpDown.Duration = (UInt16)criteria.LapTime.TotalSeconds;
                }

                m_PreviousCriteria = value;
            }
        }

        private IActivity m_Activity = null;
        private IFilterCriteria m_PreviousCriteria;
        private SplitsPlaceholderFilterCriteria m_PlaceholderCriteria = null;
    }
}